package com.zmp.kotlinnote

import com.zmp.kotlinnote.exchange.log
import org.junit.Assert.assertEquals
import org.junit.Test
import java.io.*
import java.util.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
//        newDimens("values-ldpi/dimens.xml", 0.75F)
        newDimens("values-hdpi/dimens.xml", 1.5F)
//        newDimens("values-mdpi/dimens.xml", 1F)
//        newDimens("values/dimens.xml", 1F)
//        newDimens("values-xhdpi/dimens.xml", 2F)
//        newDimens("values-xxhdpi/dimens.xml", 3F)
//        newDimens("values-xxxhdpi/dimens.xml", 4F)

//        newDimens2("values/dimens.xml", 1.5F)
    }


    private fun newDimens2(name: String, dex: Float) {
        val parentFile = File(name).parentFile
        parentFile?.run {
            if (!exists()) {
                mkdirs()
            }
        }
        var bos: BufferedWriter? = null
        try {
            bos = BufferedWriter(OutputStreamWriter(FileOutputStream(name), "utf-8"))
            bos.write("<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"PxUsage\">")
            bos.newLine()
            for (i in 1..999) {
                val v = i / dex + 0.5F
                val format = String.format("%1$.2f", v)
                bos.write("\t<dimen name=\"px$i\">${format}dp</dimen>")
                bos.newLine()
            }
            bos.write("</resources>")
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                assert(bos != null)
                bos!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
    }

    private fun newDimens(name: String, dex: Float) {
        val parentFile = File(name).parentFile
        parentFile?.run {
            if (!exists()) {
                mkdirs()
            }
        }
        var bos: BufferedWriter? = null
        try {
            bos = BufferedWriter(OutputStreamWriter(FileOutputStream(name), "utf-8"))
            bos.write("<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"PxUsage\">")
            bos.newLine()
            for (i in 1..999) {
                val v = i / dex
                val format = String.format("%1$.2f", v)
                bos.write("\t<dimen name=\"px" + i + "\">" + format + "px</dimen>")
                bos.newLine()
            }
            bos.write("</resources>")
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                assert(bos != null)
                bos!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
    }

    @Test
    public fun ccc() {
        //MASTER1

        //MASTER1

        val arrayOf = arrayOf(
            2, 2, 3, 2, 1, 4,
            3, 2, 5, 6, 7, 5
        )
        sort(arrayOf, 0, arrayOf.size - 1)

    }

    private fun sort(arr: Array<Int>, leftP: Int, rightP: Int) {
        var left = leftP
        var right = rightP
        val key = arr[left]
        while (right > left) {
            while (right > left && arr[right] >= key) {
                right--
            }
            if (arr[right] < key) {
                change(arr, right, left)
            }

            while (right > left && arr[left] <= key) {
                left++
            }
            if (arr[left] > key) {
                change(arr, right, left)
            }
        }
        Arrays.toString(arr).log()
        left.log()
        right.log()
        if (left > leftP) {
            sort(arr, leftP, left - 1)
        }

        if (right < rightP) {
            sort(arr, right + 1, rightP)
        }
    }


    private fun change(arr: Array<Int>, i: Int, j: Int) {
        val i1 = arr[i]
        arr[i] = arr[j]
        arr[j] = i1
    }

    private fun change3(arr: Array<Int>, i: Int, j: Int) {
        val i1 = arr[i]
        arr[i] = arr[j]
        arr[j] = i1
    }
}
